from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()

driver.get("https://www.baidu.com")
driver.find_element_by_link_text("新闻").click()

# 获取全部句柄
handles = driver.window_handles
# 句柄切换
driver.switch_to.window(handles[1])
driver.find_element_by_link_text("贴吧").click()
driver.find_element_by_css_selector("#pane-news > div > ul > li.hdline0 > strong > a").click()
# 句柄切换
driver.switch_to.window(handles[0])
driver.find_element_by_id("kw").send_keys("123")

print(handles, "当前句柄：", driver.current_window_handle)

sleep(2)
driver.quit()
